﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class CultureForm : Form
    {
        public CultureForm()
        {
            InitializeComponent();
        }

        tb_culture culture;
        bool insert = false;

        private void CultureForm_Load(object sender, EventArgs e)
        {
            ShowCultutures();
        }

        private void ShowCultutures()
        {
            dgvCultures.DataSource = DBClass.Cultures.Select(se => new
            {
                ID = se.ID,
                nameRus = se.Name,
                nameUzb = se.NameUz
            });

            dgvCultures.Columns["ID"].Visible = false;
            dgvCultures.Columns["nameRus"].HeaderText = "Русское название";
            dgvCultures.Columns["nameUzb"].HeaderText = "Узбек Исми";
            dgvCultures.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvCultures.AutoResizeColumns();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (insert)
            {
                if (String.IsNullOrEmpty(tbCultureName.Text) || String.IsNullOrEmpty(tbCultureNameUzb.Text))
                {
                    if (InformationClass.Lang)
                        MessageBox.Show("Обязательно заполнить оба названия Культуры!");
                    else
                        MessageBox.Show("Экиннинг икки номи тўлдирилиши шарт");
                    return;
                }

                culture = new tb_culture
                {
                    Name = tbCultureName.Text,
                    NameUz = tbCultureNameUzb.Text
                };
                DBClass.RESPContext.tb_cultures.InsertOnSubmit(culture);

                tbCultureName.Text = tbCultureNameUzb.Text = string.Empty;


            }
            else
            {
                if (culture != null)
                {
                    culture.Name = tbCultureName.Text;
                    culture.NameUz = tbCultureNameUzb.Text;
                }

                this.Close();
            }

            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                ShowCultutures();
                tbCultureName.Enabled = tbCultureNameUzb.Enabled = insert = false;
                buttonOK.Text = "OK";
                buttonCancel.Text = "Cancel";
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }

        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            tbCultureName.Enabled = tbCultureNameUzb.Enabled = insert = true;
            if (InformationClass.Lang)
                buttonOK.Text = "Добавить";
            else
                buttonOK.Text = "Қўшиш";
            buttonCancel.Text = "No";
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvCultures.RowCount > 0 && dgvCultures.CurrentRow != null)
            {
                insert = false;
                tbCultureName.Enabled = tbCultureNameUzb.Enabled = true;
                int cID = (int)dgvCultures["ID", dgvCultures.CurrentRow.Index].Value;
                culture = DBClass.Cultures.Where(wh => wh.ID == cID).SingleOrDefault();
                if (culture != null)
                {
                    tbCultureName.Text = culture.Name;
                    tbCultureNameUzb.Text = culture.NameUz;
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvCultures.RowCount > 0 && dgvCultures.CurrentRow != null)
            {
                int cID = (int)dgvCultures["ID", dgvCultures.CurrentRow.Index].Value;
                culture = DBClass.Cultures.Where(wh => wh.ID == cID).SingleOrDefault();

                string cName = InformationClass.Lang ? culture.Name : culture.NameUz;
                string msg = InformationClass.Lang ? "Вы действительно хотите удалить С/Х Культуру \"" + cName + " \"" : "Сиз ҳақиқатдан ҳам қ/х экинини олиб ташламоқчисиз \"" + cName + " \"";

                if (MessageBox.Show(msg, "Внимание!", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DBClass.Cultures.DeleteOnSubmit(culture);
                    DBClass.RESPContext.SubmitChanges();
                    ShowCultutures();
                }
                else
                {
                    culture = null;
                }

            }
        }

        private void tsbSeed_Click(object sender, EventArgs e)
        {

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
